#include "ohos_rtp_sender.h"
#include "ohos_media_track.h"
#include "ohos_audio_track.h"
#include "ohos_video_track.h"

namespace ohoswebrtc {

OHOSRtpSender::OHOSRtpSender(
    rtc::scoped_refptr<webrtc::RtpSenderInterface> rtp_sender, rtc::scoped_refptr<OHOSPeerConnectionFactoryInterface> peer_connection_factory)
    : rtp_sender_(rtp_sender),
      peer_connection_factory_(peer_connection_factory) {
  RTC_LOG(LS_INFO) << __FUNCTION__ << ": Ctor";
}

bool OHOSRtpSender::set_track(rtc::scoped_refptr<OHOSMediaTrackInterface> track) {
  if (!track) {
    return rtp_sender_->SetTrack(nullptr);
  }
  return rtp_sender_->SetTrack(track->track().get());
}


rtc::scoped_refptr<OHOSMediaTrackInterface> OHOSRtpSender::track() const {  
  rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> track =
      rtp_sender_->track();
  if (!track) {
    return rtc::scoped_refptr<OHOSMediaTrackInterface>();
  }
  if (track->kind() == webrtc::MediaStreamTrackInterface::kVideoKind) {
    return rtc::scoped_refptr<OHOSMediaTrackInterface>(new rtc::RefCountedObject<OHOSVideoTrack>(
      rtc::scoped_refptr<webrtc::VideoTrackInterface>(
            static_cast<webrtc::VideoTrackInterface*>(track.get())) ,peer_connection_factory_));
  } else if (track->kind() == webrtc::MediaStreamTrackInterface::kAudioKind) {
    return rtc::scoped_refptr<OHOSMediaTrackInterface>(new rtc::RefCountedObject<OHOSAudioTrack>(
        rtc::scoped_refptr<webrtc::AudioTrackInterface>(
            static_cast<webrtc::AudioTrackInterface*>(track.get())), peer_connection_factory_));
  }
  return rtc::scoped_refptr<OHOSMediaTrackInterface>();
}

void OHOSRtpSender::SetEncoderSelector(
      std::unique_ptr<webrtc::VideoEncoderFactory::EncoderSelectorInterface>
          encoder_selector) {
  rtp_sender_->SetEncoderSelector(std::move(encoder_selector));
}

OHOSRtpSender::~OHOSRtpSender() {
  RTC_LOG(LS_INFO) << __FUNCTION__ << ": Dtor";
}



}  // namespace ohoswebrtc